package com.yun.lc.control;


import com.yun.lc.modal.UserLoginModal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserLoginServlet", urlPatterns = {"/UserLoginServlet"})
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String strAdminName = request.getParameter("adminName");// 参数名要和form表单中的name属性值保持一致
        String strAdminPsw = request.getParameter("adminPsw");
        System.out.println("登录用户名：" + strAdminName + ";密码：" + strAdminPsw);


        UserLoginModal userM = new UserLoginModal();
        boolean isOk = userM.isLogined(strAdminName, strAdminPsw);


        if (isOk) {
            request.getSession().setAttribute("loginedName", strAdminName);  // 缓存到session作用域对象中
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else {

            response.sendRedirect("index.jsp");
        }
    }
}